package com.wze.wze.Controller;

import com.wze.wze.Aop.PasToken;
import com.wze.wze.Entity.ResultEntity;
import com.wze.wze.Entity.Singer;
import com.wze.wze.Entity.UserTab;
import com.wze.wze.Entity.v_pl;
import com.wze.wze.Service.SingerService;
import com.wze.wze.Util.JSONTool;
import com.wze.wze.Util.OSSUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@CrossOrigin //解决跨域
@RestController //控制器注解
@RequestMapping("/Singer/")//请求路径
public class SingerController {
    @Resource
    SingerService singerService;
    ResultEntity resultEntity = null;



    @GetMapping("Singer")
    private String getUserById(@RequestParam("SName") String SName) {
        resultEntity = new ResultEntity();
        try {
            Singer singer = singerService.getSingerById(SName);
            if (singer == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(singer);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);

    }


    @GetMapping("ISinger")
    private String getSingerById(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            Singer singer = singerService.getSingeById(id);
            if (singer == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(singer);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);

    }

    @PostMapping("Singer")
    private String addSinger(@RequestParam("SName") String SName, @RequestParam("SInfo") String SInfo,@RequestParam("file") MultipartFile SImgFile) {
        resultEntity = new ResultEntity();
        OSSUtil ossUtil = new OSSUtil();
        String FilePath="D:\\BiShe\\";//存储Excel的位置
        String SuffixName = SImgFile.getOriginalFilename().substring(SImgFile.getOriginalFilename().lastIndexOf("."));//获取文件后缀
        String FileNewName= UUID.randomUUID().toString()+SuffixName;//新的文件名
        File dest = new File(FilePath+FileNewName);//生成新的文件
        if(!dest.getParentFile().exists()){//如果路径不存在，就生成
            dest.getParentFile().mkdirs();
        }
        try{
            SImgFile.transferTo(dest);//将临时文件保存到对应的路径
        }catch (Exception e){
            e.printStackTrace();
        }
        String OSSFilePath = ossUtil.updateFile(FilePath+FileNewName,FileNewName);
            try {
                if (singerService.addSinger(SName,SInfo,OSSFilePath) > 0) {
                    resultEntity.setCode("0");
                    resultEntity.setMsg("添加成功！");
                } else {
                    resultEntity.setCode("1");
                    resultEntity.setMsg("添加失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultEntity.setCode("9999");
                resultEntity.setMsg("系统错误，请联系管理员");
            }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return json
     */
    @DeleteMapping("Singer")
    private String delSinger(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            if (singerService.delSingerById(id) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PutMapping("Singer")
    private String upUser(@RequestParam("SName") String SName, @RequestParam("SInfo") String SInfo, @RequestParam("file") MultipartFile SImgFile, @RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        OSSUtil ossUtil = new OSSUtil();
        String FilePath="D:\\BiShe\\";//存储Excel的位置
        String SuffixName = SImgFile.getOriginalFilename().substring(SImgFile.getOriginalFilename().lastIndexOf("."));//获取文件后缀
        String FileNewName= UUID.randomUUID().toString()+SuffixName;//新的文件名
        File dest = new File(FilePath+FileNewName);//生成新的文件
        if(!dest.getParentFile().exists()){//如果路径不存在，就生成
            dest.getParentFile().mkdirs();
        }
        try{
            SImgFile.transferTo(dest);//将临时文件保存到对应的路径
        }catch (Exception e){
            e.printStackTrace();
        }
        String OSSFilePath = ossUtil.updateFile(FilePath+FileNewName,FileNewName);
            try {
                Singer singer = new Singer();
                singer.setId(id);
                singer.setSName(SName);
                singer.setSInfo(SInfo);
                singer.setSImg(OSSFilePath);
                if (singerService.upSinger(singer) > 0) {
                    resultEntity.setCode("0");
                    resultEntity.setMsg("修改成功！");
                } else {
                    resultEntity.setCode("1");
                    resultEntity.setMsg("修改失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultEntity.setCode("9999");
                resultEntity.setMsg("系统错误，请联系管理员");
            }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PasToken
    @GetMapping("SingerList")
    private String PlList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("id")  int id,
            @RequestParam("SName")  String SName
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<Singer> list = singerService.getSingerList(Start, limit, id, SName);
            if(id!=-1 || !SName.equals("")){//有条件
                resultEntity.setCount(String.valueOf(list.size()));
            }else {//没有查询条件
                resultEntity.setCount(String.valueOf(singerService.getSingerCount()));
            }
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }
}
